/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.gui.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.InvocationException;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
//import com.google.gwt.user.client.ui.*;

import java.util.*;

import nfse.gui.client.data.*;

import com.gwtext.client.widgets.grid.GridPanel;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.*;
import com.gwtext.client.widgets.event.*;

/**
 * This class handles the generation of the Dashboard tab. This tab is made up
 * of the following sections:
 * 
 * 1. Critical Servers Status 2. Disk Space Usage 3. Event Stats 4. A tabbed
 * panel holding snap shots of each Data Type and a Rotational View tab of all
 * the Data Types
 * 
 * The sections are currently laid out as follows
 * 
 * |--------|--------| | | 2 | | 1 |--------| | | 3 | |--------|------- | | | |
 * 4 | |----------------|
 * 
 * The current layout uses all GWT-Ext classes except for the container class
 * being an extension of FlexTable. This is for backward compatibility with the
 * existing nfse.gui.client.NetFSEClient. At some point would like to drop GWT
 * specific classes altogether.
 * 
 * The class is required to refresh the data in each section based on a set
 * cycle defined for each section. The cycle time is configurable.
 * 
 * Each section will be a GWT-Ext panel. To get the desired layout appropriate
 * layout classes will be used in each panel.
 * 
 * The snap shot tabbed panel will consist of a tab for each Data Type and a tab
 * for the rotational view. The rotational view tab will be the first tab in the
 * tabbed panel.
 * 
 * 
 * Design
 * 
 * 1. Initializer methods to create each panel 2. refresh methods to refresh
 * specific panels based on a cycle time 3. Rotation method for handling the
 * rotation of the Data Types in the rotation tab of the tabbed panel 4. A build
 * whole panel method to be returned to the NetFSEClient 5. Each of the sections
 * are created from a separate class which extends panel
 * 
 * 
 * 
 * 
 * @author john
 * 
 */
//public class HeadsUpTab extends FlexTable {

public class HeadsUpTab extends com.gwtext.client.widgets.Panel 
{

    private NetFSEClient client = null;
    private final int width = 1000;

    private HashMap snapShotMap;

    private boolean useTestData = false;

    private String snapShotTimeInterval = " 30 Minute SnapShot";

    private final com.gwtext.client.widgets.Panel mainPanel = new com.gwtext.client.widgets.Panel();

    private final com.gwtext.client.widgets.Panel topPanel = new com.gwtext.client.widgets.Panel();

    private final com.gwtext.client.widgets.Panel rightTopPanel = new com.gwtext.client.widgets.Panel();

    private final com.gwtext.client.widgets.Panel bottomPanel = new com.gwtext.client.widgets.Panel();

    private final int criticalServersGridHeight = 400;

    private final int criticalServersGridWidth = 500;

    private final CriticalServersGrid criticalServersGrid = new CriticalServersGrid(criticalServersGridHeight,
            criticalServersGridWidth);

    private final int diskSpaceUsageGridHeight = 150;

    private final int diskSpaceUsageGridWidth = 500;

    private final DiskSpaceUsageGrid diskSpaceUsageGrid = new DiskSpaceUsageGrid(diskSpaceUsageGridHeight,
            diskSpaceUsageGridWidth);

    private final int eventStatsGridHeight = 220;

    private final int eventStatsGridWidth = 500;

    private final EventStatsGrid eventStatsGrid = new EventStatsGrid(eventStatsGridHeight, eventStatsGridWidth);

    private final int snapShotPanelHeight = 500;

    private final int snapShotPanelWidth = 1000;

    private final SnapShotPanel snapShotPanel = new SnapShotPanel(snapShotPanelHeight, snapShotPanelWidth);

    private NetFSEClient netFSEClient = null;

    private boolean criticalServersRPCComplete = false;

    private boolean eventStatsRPCComplete = false;

    private boolean diskUsageRPCComplete = false;

    private boolean snapShotRPCComplete = false;

    private final ArrayList columns = new ArrayList();

    private HashMap sensorTypesMap;

    ToolbarButton autoRefreshEnable;

    private boolean autoRefreshEnabled = true;

    private int refreshFrequency = 120; // 60 is 1 minute * factor in each
                                        // refresh. ex refreshFrequency * 2 *
                                        // 1000 = 2 minutes

    private HashMap eventStatsMap = new HashMap();

    private HashMap criticalServersMap = new HashMap();

    private CriticalServerEntries criticalServerEntries = new CriticalServerEntries();

    // private boolean useDashboardTestData = true;

    private ArrayList diskUsageList = new ArrayList();

    private boolean useDashboardTestData = false;

    // Add a element for each piece of the dashboard that requires a Async
    // method call in this class
    // This assures that the dashboard is not built until all the pieces are
    // complete
    private boolean[] dashboardBuildCheck = { false, false, false, false };

    private boolean checkForDashboardBuild() {
        for (int i = 0; i < dashboardBuildCheck.length; i++) {
            if (!dashboardBuildCheck[i]) {
                dashboardBuildCheck[i] = true;
                if (i == dashboardBuildCheck.length - 1)
                    return true;
                else
                    return false;
            }
        }

        return false;
    }

    public HeadsUpTab(int refreshFrequency, NetFSEClient client) {
        this.client = client;
        this.setTitle("Heads Up");
        if (refreshFrequency != 0)
            this.refreshFrequency = refreshFrequency;
        
        if (!useDashboardTestData) {
            this.getSensorTypes();
        } else {
            setUseTestData(useDashboardTestData);
            buildComponentsWithTestData();
            buildPage();
        }
    }

    public void buildPage() {
        buildPage(netFSEClient);
    }

    public void buildPage(NetFSEClient netFSEClient) {
        this.netFSEClient = netFSEClient;
        mainPanel.setLayout(new com.gwtext.client.widgets.layout.VerticalLayout());
        mainPanel.setWidth(width + 1);

        ButtonListenerAdapter listener = new ButtonListenerAdapter() {
            public void onClick(Button button, com.gwtext.client.core.EventObject e) {
                System.out.println("The auto refresh disable button was clicked");
                String enable = autoRefreshEnable.getText();
                if (enable.equals("Enable Auto-Refresh")) {
                    autoRefreshEnable.setText("Disable Auto-Refresh");
                    autoRefreshEnabled = true;
                    System.out.println("the enable was hit");
                } else {
                    autoRefreshEnable.setText("Enable Auto-Refresh");
                    autoRefreshEnabled = false;
                    System.out.println("the disable was hit");
                }

            }
        };

        autoRefreshEnable = new ToolbarButton("Disable Auto-Refresh", listener);
        mainPanel.setBottomToolbar(autoRefreshEnable);

        topPanel.setLayout(new com.gwtext.client.widgets.layout.HorizontalLayout(0));
        topPanel.setWidth(width);

        topPanel.add(criticalServersGrid);

        rightTopPanel.setLayout(new com.gwtext.client.widgets.layout.VerticalLayout());
        rightTopPanel.setWidth(501);
        rightTopPanel.setHeight(352); // The extra 2 pxs were needed to close
                                        // a gap in the panel

        rightTopPanel.add(diskSpaceUsageGrid);
        rightTopPanel.add(eventStatsGrid);

        topPanel.add(rightTopPanel);

        //mainPanel.add(topPanel);

        bottomPanel.add(snapShotPanel);

        bottomPanel.setWidth(width);

        mainPanel.add(bottomPanel);
        mainPanel.add(topPanel);


        //this.setWidget(0, 0, mainPanel);
        this.add(mainPanel);
        //this.setWidth(width + 2 + "px");

        // startSnapShotRotation();
    }

    public void getEventStats(String timePeriod) {

        ReportServiceAsync nfseService = (ReportServiceAsync) GWT.create(ReportService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.REPORT_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                System.out.println("Failed the getEventStats callback with " + caught.getMessage());
                caught.printStackTrace();

                try {
                    throw caught;
                } catch (InvocationException e) {
                    System.out.println("Failed the getEventStats callback with invocation " + e.getMessage());
                    e.printStackTrace();

                } catch (Throwable e) {
                    // last resort -- a very unexpected exception
                    System.out.println("What now " + e.getMessage());
                }
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                // eventStatsMap = (HashMap) result;
                setEventStatsResult(result);
            }
        };

        nfseService.getEventStats(timePeriod, callback);

    }

    private void setSensorTypesResult(Object resultsSet) {
        this.setSensorTypesMap((HashMap) resultsSet);
        this.setCriticalServersRPCComplete(false);
        this.setDiskUsageRPCComplete(false);
        this.setEventStatsRPCComplete(false);
        this.setSnapShotRPCComplete(false);
        this.getEventStats(null);
        this.getDiskUsage();
        this.getCriticalServers(null);
        this.getSnapShots(null);
    }

    private void setSnapShotResult(Object resultSet) {
        this.setSnapShotRPCComplete(true);
        this.buildSnapShotPanel((SnapShotData) resultSet);

        if (checkForDashboardBuild()) {
            this.buildPage(client);
        }
    }

    public void getCriticalServers(String timePeriod) {
        ReportServiceAsync nfseService = (ReportServiceAsync) GWT.create(ReportService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.REPORT_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                System.out.println("Failed the getCriticalServers callback with " + caught.getMessage());
                caught.printStackTrace();

                try {
                    throw caught;
                } catch (InvocationException e) {
                    System.out.println("Failed the getCriticalServers callback with invocation " + e.getMessage());
                    e.printStackTrace();

                } catch (Throwable e) {
                    // last resort -- a very unexpected exception
                    System.out.println("What now " + e.getMessage());
                }
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                setCriticalServersResult(result);
            }
        };

        nfseService.getCriticalServers(timePeriod, callback);
    }

    public void getDiskUsage() {
        ReportServiceAsync nfseService = (ReportServiceAsync) GWT.create(ReportService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.REPORT_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                System.out.println("Failed the getDiskUsage callback with " + caught.getMessage());
                caught.printStackTrace();

                try {
                    throw caught;
                } catch (InvocationException e) {
                    System.out.println("Failed the getDiskUsage callback with invocation " + e.getMessage());
                    e.printStackTrace();

                } catch (Throwable e) {
                    // last resort -- a very unexpected exception
                    System.out.println("What now " + e.getMessage());
                }
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                setDiskUsageResult(result);
            }
        };

        nfseService.getDiskUsage(callback);
    }

    private void setCriticalServersResult(Object resultSet) {
        this.setCriticalServersRPCComplete(true);
        this.buildCriticalServersGrid((CriticalServerEntries) resultSet);
        // headsUpTable.buildCriticalServersGrid((HashMap)resultSet);
        if (checkForDashboardBuild()) {
            this.buildPage(client);
        }
    }

    private void setDiskUsageResult(Object resultSet) {
        this.setDiskUsageRPCComplete(true);
        this.buildDiskUsage((ArrayList) resultSet);

        if (checkForDashboardBuild()) {
            this.buildPage(client);
        }
    }

    private void setEventStatsResult(Object resultSet) {
        this.setEventStatsRPCComplete(true);
        this.buildEventStatsGrid((HashMap) resultSet);
        if (checkForDashboardBuild()) {
            this.buildPage(client);
        }

    }

    public void getSensorTypes() {
        ReportServiceAsync nfseService = (ReportServiceAsync) GWT.create(ReportService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.REPORT_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                System.out.println("Failed the getSensorTypes callback with " + caught.getMessage());
                caught.printStackTrace();

                try {
                    throw caught;
                } catch (InvocationException e) {
                    System.out.println("Failed the getSnapShots callback with invocation " + e.getMessage());
                    e.printStackTrace();

                } catch (Throwable e) {
                    // last resort -- a very unexpected exception
                    System.out.println("What now " + e.getMessage());
                }
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                setSensorTypesResult(result);
            }
        };

        nfseService.getSensorTypes(callback);
    }

    public void getSnapShots(String timePeriod) {
        ReportServiceAsync nfseService = (ReportServiceAsync) GWT.create(ReportService.class);

        ServiceDefTarget endpoint = (ServiceDefTarget) nfseService;
        endpoint.setServiceEntryPoint(NetFSEClient.REPORT_URL);

        AsyncCallback callback = new AsyncCallback() {
            public void onFailure(Throwable caught) {
                System.out.println("Failed the getSnapShots callback with " + caught.getMessage());
                caught.printStackTrace();

                try {
                    throw caught;
                } catch (InvocationException e) {
                    System.out.println("Failed the getSnapShots callback with invocation " + e.getMessage());
                    e.printStackTrace();

                } catch (Throwable e) {
                    // last resort -- a very unexpected exception
                    System.out.println("What now " + e.getMessage());
                }
                NetFSEClient.redirect("NetFSEClient.html");
            }

            public void onSuccess(Object result) {
                setSnapShotResult(result);
            }
        };

        nfseService.getSnapShotStats(timePeriod, callback);
    }

    public void buildDiskUsage(ArrayList diskUsageList) {
        if (useTestData) {
            diskUsageList = getTestDiskSpaceUsageList();
        }

        if (diskSpaceUsageGrid.getGrid() == null) {
            diskSpaceUsageGrid.buildGrid(diskUsageList, this);
            startDiskUsageRotation();
        } else
            diskSpaceUsageGrid.refreshGrid(diskUsageList);
    }

    // public void buildCriticalServersGrid(HashMap criticalServersMap)
    public void buildCriticalServersGrid(CriticalServerEntries criticalServerEntries) {
        if (useTestData) {
            this.sensorTypesMap = this.createTestSensorTypesHashMap();
            // criticalServersMap = getTestCriticalServersMap();
            criticalServerEntries = this.getTestCriticalServersMap();
        }

        if (criticalServersGrid.getGrid() == null) {
            criticalServersGrid.buildGrid(criticalServerEntries, this);
            startCriticalServerRotation();
        } else
            criticalServersGrid.refreshGrid(criticalServerEntries);
    }

    public void buildEventStatsGrid(HashMap eventStatsMap) {
        if (useTestData) {
            this.sensorTypesMap = this.createTestSensorTypesHashMap();
            eventStatsMap = this.getTestEventStatsMap();
        }

        if (eventStatsGrid.getGrid() == null) {
            eventStatsGrid.buildGrid(eventStatsMap, this);
            startEventStatsRotation();
        } else
            eventStatsGrid.refreshGrid(eventStatsMap);
    }

    public void buildSnapShotPanel(SnapShotData snapShotData) {
        if (useTestData) {
            this.sensorTypesMap = this.createTestSensorTypesHashMap();
            snapShotMap = this.getTestSnapShotMap();
        }

        if (snapShotPanel.getTabPanel() == null) {
            snapShotPanel.buildSnapShotTabPanel(snapShotData, this);
            startSnapShotRotation();
            startSnapShotRefreshRotation();
        } else
            snapShotPanel.refreshTabs(snapShotData);
    }

    public void refreshEventStatsGrid() {
        getEventStats(null);
    }

    public void refreshDiskUsage() {
        getDiskUsage();
    }

    public void refreshCriticalServersGrid() {
        getCriticalServers(null);
    }

    public void refreshSnapShotPanel() {
        getSnapShots(null);
    }

    public void buildComponentsWithTestData() {
        buildCriticalServersGrid(null);
        buildEventStatsGrid(null);
        buildDiskUsage(null);
        buildSnapShotPanel(null);
    }

    private void startSnapShotRotation() {
        com.google.gwt.user.client.Timer timer = new com.google.gwt.user.client.Timer() {
            public void run() {
                if (snapShotPanel != null) {
                    System.out.println("Snap Shot Tab Rotation");
                    snapShotPanel.getNewRotationalView();
                }
            }
        };
        // timer.scheduleRepeating(60 * 1000);
        timer.scheduleRepeating(refreshFrequency * 1 * 1000);
    }

    private void startCriticalServerRotation() {

        com.google.gwt.user.client.Timer timer = new com.google.gwt.user.client.Timer() {
            public void run() {
                if ((criticalServersGrid != null) && (isCriticalServersRPCComplete()) && autoRefreshEnabled) {
                    System.out.println("Critical Server Refresh");
                    setCriticalServersRPCComplete(false);
                    getCriticalServers(null);
                }
            }
        };
        // timer.scheduleRepeating(30 * 1000);
        timer.scheduleRepeating(refreshFrequency * 3 * 1000);
    }

    private void startDiskUsageRotation() {

        com.google.gwt.user.client.Timer timer = new com.google.gwt.user.client.Timer() {
            public void run() {
                if ((diskSpaceUsageGrid != null) && (isDiskUsageRPCComplete()) && autoRefreshEnabled) {
                    System.out.println("Disk Usage Refresh");
                    setDiskUsageRPCComplete(false);
                    getDiskUsage();
                }
            }
        };
        // timer.scheduleRepeating(40 * 1000);
        timer.scheduleRepeating(refreshFrequency * 10 * 1000);
    }

    private void startEventStatsRotation() {

        com.google.gwt.user.client.Timer timer = new com.google.gwt.user.client.Timer() {
            public void run() {
                if ((eventStatsGrid != null) && (isEventStatsRPCComplete()) && autoRefreshEnabled) {
                    System.out.println("Event Stats Refresh");
                    setEventStatsRPCComplete(false);
                    getEventStats(null);
                }
            }
        };
        // timer.scheduleRepeating(50 * 1000);
        timer.scheduleRepeating(refreshFrequency * 4 * 1000);
    }

    private void startSnapShotRefreshRotation() {

        com.google.gwt.user.client.Timer timer = new com.google.gwt.user.client.Timer() {
            public void run() {
                if ((snapShotPanel != null) && (isSnapShotRPCComplete()) && autoRefreshEnabled) {
                    System.out.println("Snap Shot Refresh");
                    setSnapShotRPCComplete(false);
                    getSnapShots(null);
                }
            }
        };
        // timer.scheduleRepeating(60 * 1000);
        timer.scheduleRepeating(refreshFrequency * 5 * 1000);
    }

    /**
     * Handles getting the snap shot time interval. For example default is a 30
     * minute snap shot
     * 
     * @return
     */
    public String getSnapShotTimeInterval() {
        return snapShotTimeInterval;
    }

    public void setSnapShotTimeInterval(String snapShotTimeInterval) {
        this.snapShotTimeInterval = snapShotTimeInterval;
    }

    private HashMap getSnapShotMap() {
        return snapShotMap;
    }

    public void setSnapShotData(HashMap snapShotMap) {
        this.snapShotMap = snapShotMap;
    }

    public boolean isUseTestData() {
        return useTestData;
    }

    public void setUseTestData(boolean useTestData) {
        this.useTestData = useTestData;
    }

    public HashMap getSensorTypesMap() {
        return sensorTypesMap;
    }

    public void setSensorTypesMap(HashMap sensorTypesMap) {
        this.sensorTypesMap = sensorTypesMap;
    }

    private HashMap createTestSensorTypesHashMap() {
        HashMap localMap = new HashMap();

        SensorTypes sTypes = new SensorTypes();
        sTypes.setTypeId("1");
        sTypes.setTypeName("Snort");
        localMap.put(sTypes.getTypeId(), sTypes);

        sTypes = new SensorTypes();
        sTypes.setTypeId("2");
        sTypes.setTypeName("Firewall");
        localMap.put(sTypes.getTypeId(), sTypes);

        sTypes = new SensorTypes();
        sTypes.setTypeId("3");
        sTypes.setTypeName("ProofPoint");
        localMap.put(sTypes.getTypeId(), sTypes);

        sTypes = new SensorTypes();
        sTypes.setTypeId("4");
        sTypes.setTypeName("Syslog");
        localMap.put(sTypes.getTypeId(), sTypes);

        sTypes = new SensorTypes();
        sTypes.setTypeId("5");
        sTypes.setTypeName("NetFlow");
        localMap.put(sTypes.getTypeId(), sTypes);

        return localMap;
    }

    private HashMap getTestEventStatsMap() {
        HashMap localMap = new HashMap();

        EventStats eventStats = new nfse.gui.client.data.EventStats();
        eventStats.setTypeId("1");
        // eventStats.setSensor("4");
        eventStats.setNewestEvent("2008-03-09 03:20:10");
        eventStats.setOldestEvent("2008-03-07 10:01:09");
        eventStats.setEventCount("4812");
        localMap.put(eventStats.getTypeId() + eventStats.getSensor(), eventStats);

        eventStats = new nfse.gui.client.data.EventStats();
        eventStats.setTypeId("2");
        // eventStats.setSensor("3");
        eventStats.setNewestEvent("2008-03-07 15:23:10");
        eventStats.setOldestEvent("2008-03-05 14:50:00");
        eventStats.setEventCount("2327");
        localMap.put(eventStats.getTypeId() + eventStats.getSensor(), eventStats);

        eventStats = new nfse.gui.client.data.EventStats();
        eventStats.setTypeId("3");
        // eventStats.setSensor("2");
        eventStats.setNewestEvent("2008-03-09 12:45:00");
        eventStats.setOldestEvent("2008-03-05 10:10:00");
        eventStats.setEventCount("1008");
        localMap.put(eventStats.getTypeId() + eventStats.getSensor(), eventStats);

        eventStats = new nfse.gui.client.data.EventStats();
        eventStats.setTypeId("4");
        // eventStats.setSensor("1");
        eventStats.setNewestEvent("2008-03-09 01:03:40");
        eventStats.setOldestEvent("2008-03-05 10:30:43");
        eventStats.setEventCount("474");
        localMap.put(eventStats.getTypeId() + eventStats.getSensor(), eventStats);

        eventStats = new nfse.gui.client.data.EventStats();
        eventStats.setTypeId("5");
        // eventStats.setSensor("5");
        eventStats.setNewestEvent("2008-03-09 12:45:00");
        eventStats.setOldestEvent("2008-03-05 01:32:20");
        eventStats.setEventCount("10802");
        localMap.put(eventStats.getTypeId() + eventStats.getSensor(), eventStats);

        return localMap;
    }

    private CriticalServerEntries getTestCriticalServersMap() {

        String[] addresses = { "141.0.1.150", "123.45.23.12", "90.120.234.10", "56.0.1.112", "201.23.126.252",
                "151.89.34.10", "15.0.1.100", "123.45.230.112", "90.120.231.10", "90.120.234.12" };
        int[] fudgeFactor = { 10000, 100000, 1000, 1000, 1000000, 100, 1000 };
        CriticalServerEntries cse = new CriticalServerEntries();

        String localTypes[] = { "Snort", "Firewall", "Proofpoint", "Syslog", "Netflow" };
        cse.typeNames = localTypes;

        cse.entries = new CriticalServer[addresses.length];
        for (int i = 0; i < addresses.length; i++) {
            cse.entries[i] = new CriticalServer();
            cse.entries[i].setIp(addresses[i]);
            cse.entries[i].counts = new long[5];
            for (int j = 0; j < 5; j++) {
                double d = Math.random() * fudgeFactor[j];
                cse.entries[i].counts[j] = (long) d;
            }
        }

        return cse;
    }

    // private HashMap getTestCriticalServersMap2()
    // {
    //		
    // String[] addresses =
    // {"141.0.1.150","123.45.23.12","90.120.234.10","56.0.1.112","201.23.126.252","151.89.34.10","15.0.1.100","123.45.230.112","90.120.231.10","90.120.234.12"};
    // int[] fudgeFactor = {10000,100000,1000,1000,1000000};
    // HashMap localMap = new HashMap();
    //		
    // for (int i = 0; i < addresses.length; i++)
    // {
    // ArrayList ipRecord = new ArrayList();
    // for (int j = 0; j < 5; j++)
    // {
    // CriticalServer lcs = new CriticalServer();
    // double d = Math.random() * fudgeFactor[j];
    // // lcs.setDataType((j + 1) + "");
    // // lcs.setCount((int)d + "");
    // ipRecord.add(lcs);
    // }
    // localMap.put(addresses[i], ipRecord);
    // }
    //    	
    // return localMap;
    // }

    private HashMap getTestSnapShotMap() {

        String[] srcAddresses = { "141.0.1.150", "123.45.23.12", "90.120.234.10", "56.0.1.112", "201.23.126.252",
                "151.89.34.10", "15.0.1.100", "123.45.230.112", "90.120.231.10", "90.120.234.12" };
        String[] dstAddresses = { "141.0.1.150", "123.45.23.12", "100.10.44.90", "56.0.1.112", "201.23.126.252",
                "151.89.34.10", "15.0.1.100", "123.45.230.112", "90.120.231.10", "90.120.234.12" };
        String[] portAddresses = { "80", "8080", "8443", "23", "25", "4040", "9090", "9091", "12000", "3000" };
        int[] fudgeFactor = { 10000, 100000, 1000, 1000, 1000000 };

        HashMap returnMap = new HashMap();

        Iterator it = sensorTypesMap.keySet().iterator();
        int i = 0;
        while (it.hasNext()) {
            SensorTypes sType = (SensorTypes) sensorTypesMap.get((String) it.next());
            HashMap localMap = new HashMap();

            ArrayList topSourcesList = new ArrayList();
            for (int j = 0; j < 10; j++) {
                SnapShotDataTypeCategoryRecord ssData = new SnapShotDataTypeCategoryRecord();
                ssData.setAddress(srcAddresses[j]);
                double d = Math.random() * fudgeFactor[i];
                ssData.setHits((int) d + "");
                topSourcesList.add(ssData);
            }
            Collections.sort(topSourcesList, new HitComparator());
            localMap.put("Top Sources", topSourcesList);

            ArrayList topDesList = new ArrayList();
            for (int j = 0; j < 10; j++) {
                SnapShotDataTypeCategoryRecord ssData = new SnapShotDataTypeCategoryRecord();
                ssData.setAddress(dstAddresses[j]);
                double d = Math.random() * fudgeFactor[i];
                ssData.setHits((int) d + "");
                topDesList.add(ssData);
            }
            Collections.sort(topDesList, new HitComparator());
            localMap.put("Top Targets", topDesList);

            ArrayList topPortList = new ArrayList();
            for (int j = 0; j < 10; j++) {
                SnapShotDataTypeCategoryRecord ssData = new SnapShotDataTypeCategoryRecord();
                ssData.setPort(portAddresses[j]);
                double d = Math.random() * 1000000;
                ssData.setHits((int) d + "");
                topPortList.add(ssData);
            }
            Collections.sort(topPortList, new HitComparator());
            localMap.put("Top Ports", topPortList);
            i++;
            returnMap.put(sType.getTypeId(), localMap);
        }
        return returnMap;

    }

    private ArrayList getTestDiskSpaceUsageList() {
        ArrayList aList = new ArrayList();

        DiskSpace diskSpace1 = new DiskSpace();
        diskSpace1.setTotalDiskSpace("226129108");
        diskSpace1.setSpaceUsed("10731864");
        diskSpace1.setSpaceAvailable("203725284");
        diskSpace1.setPercentUsed("6%");
        diskSpace1.setDataDirectory("/data");
        aList.add(diskSpace1);

        DiskSpace diskSpace2 = new DiskSpace();
        diskSpace2.setTotalDiskSpace("194449");
        diskSpace2.setSpaceUsed("17430");
        diskSpace2.setSpaceAvailable("166979");
        diskSpace2.setPercentUsed("10%");
        diskSpace2.setDataDirectory("/dev/sda3");
        aList.add(diskSpace2);

        return aList;

    }

    class HitComparator implements Comparator {
        public int compare(Object o1, Object o2) {
            SnapShotDataTypeCategoryRecord ssData1 = (SnapShotDataTypeCategoryRecord) o1;
            SnapShotDataTypeCategoryRecord ssData2 = (SnapShotDataTypeCategoryRecord) o2;

            int s1 = new Integer(ssData1.getHits()).intValue();
            int s2 = new Integer(ssData2.getHits()).intValue();
            if (s1 < s2)
                return 1;
            else if (s1 == s2)
                return 0;
            else
                return -1;
        }
    }

    public boolean isCriticalServersRPCComplete() {
        return criticalServersRPCComplete;
    }

    public void setCriticalServersRPCComplete(boolean criticalServersRPCComplete) {
        this.criticalServersRPCComplete = criticalServersRPCComplete;
    }

    public boolean isEventStatsRPCComplete() {
        return eventStatsRPCComplete;
    }

    public void setEventStatsRPCComplete(boolean eventStatsRPCComplete) {
        this.eventStatsRPCComplete = eventStatsRPCComplete;
    }

    public boolean isDiskUsageRPCComplete() {
        return diskUsageRPCComplete;
    }

    public void setDiskUsageRPCComplete(boolean diskUsageRPCComplete) {
        this.diskUsageRPCComplete = diskUsageRPCComplete;
    }

    public boolean isSnapShotRPCComplete() {
        return snapShotRPCComplete;
    }

    public void setSnapShotRPCComplete(boolean snapShotRPCComplete) {
        this.snapShotRPCComplete = snapShotRPCComplete;
    }

    public void openQueryPanel(String queryId) {
        this.netFSEClient.openQuery(queryId);
    }
}
